package com.iocollege.tinyframe.service.system.impl;

import com.iocollege.tinyframe.common.core.constant.UserConstants;
import com.iocollege.tinyframe.common.service.impl.BaseMyService;
import com.iocollege.tinyframe.common.utils.StringUtils;
import com.iocollege.tinyframe.entity.system.SysPost;
import com.iocollege.tinyframe.mapper.system.SysPostMapper;
import com.iocollege.tinyframe.mapper.system.SysUserPostMapper;
import com.iocollege.tinyframe.service.system.SysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iocollege.tinyframe.service.system.SysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Service
public class SysPostServiceImpl extends BaseMyService<SysPostMapper, SysPost> implements SysPostService {

    @Autowired
    private SysUserPostService sysUserPostService;


    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostCodeUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getId()) ? -1L : post.getId();
        SysPost info = this.findOndeByAttrName("post_code",post.getPostCode());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Integer> selectPostListByUserId(Long userId)
    {
        return this.baseMapper.selectPostListByUserId(userId);
    }




    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostNameUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getId()) ? -1L : post.getId();
        SysPost info = this.findOndeByAttrName("post_name",post.getPostName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId) {
        return  sysUserPostService.countByAttrName("post_id",postId);
    }


}
